package com.atguigu.flink03;

import com.atguigu.beans.WaterSensor;
import com.atguigu.func.MyMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/2/20
 * 该案例演示了Map算子
 *      实现了提取WaterSensor中的id字段的功能。
 */
public class Flink08_Trans_Map {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<WaterSensor> stream = env.fromElements(
                new WaterSensor("sensor_1", 1L, 1),
                new WaterSensor("sensor_2", 2L, 2)
        );

        //从流对象中提取传感器的id         WaterSensor->String
        //map算子需要传递 MapFunction处理函数的实现类对象
        /*//匿名内部类
        stream.map(
                new MapFunction<WaterSensor, String>() {
                    @Override
                    public String map(WaterSensor ws) throws Exception {
                        return ws.id;
                    }
                }
        ).print();
        //lambda表达式
        stream.map(
                ws->ws.id
        ).print();

        //方法的引用
        stream.map(WaterSensor::getId).print();
        */
        //自定义函数类
        stream.map(new MyMapFunction()).print();

        env.execute();

    }
}


